﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bike.Repository;
using System.Linq.Expressions;

namespace Bike.Biz
{
    public class BikeService<T>:IBikeService<T> where  T:class
    {

        public virtual T Add(T entity)
        {
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new Repository<T>(contextAdapter);
            repository.Add(entity);
            unitOfWork.Commit();
            return entity;
        }

      
        public void Attach(T entity)
        {
            IQueryable<T> query;
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new Repository<T>(contextAdapter);

            try
            {
                repository.Attach(entity);
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                unitOfWork.Commit();
            }

        }
        public void UpdateChanges()
        {
            IQueryable<T> query;
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new Repository<T>(contextAdapter);
            
            try
            {
                //repository.Attach(entity);
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                unitOfWork.Commit();
            }

        }

        public IQueryable<T> GetQuery()
        {
            IQueryable<T> query;
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new Repository<T>(contextAdapter);

            try
            {
              query = repository.AsQueryable();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                unitOfWork.Commit();
            }
            return query;
        }


        public IEnumerable<T> GetAllItems()
        {
            IEnumerable<T> query;
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new Repository<T>(contextAdapter);

            try
            {
                query = repository.GetAll();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                unitOfWork.Commit();
            }
            return query;
        }

        public T First(Expression<Func<T, bool>> where)
        {
            T query;
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new Repository<T>(contextAdapter);

            try
            {
                query = repository.First(where);
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                unitOfWork.Commit();
            }
            return query;
        }

    }
}
